<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="学号" prop="no">
      <el-input v-model="dataForm.no" placeholder="学号"></el-input>
    </el-form-item>
    <el-form-item label="学生姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="学生姓名"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-select v-model="dataForm.sex" placeholder="请选择">
        <el-option
          v-for="item in genderOptions"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="出生日期" prop="birthday">
      <el-date-picker
      v-model="dataForm.birthday"
      type="date"
      placeholder="选择日期">
    </el-date-picker>
    </el-form-item>
    <el-form-item label="民族" prop="nation">
      <el-input v-model="dataForm.nation" placeholder="民族"></el-input>
    </el-form-item>
    <el-form-item label="学生籍贯" prop="nativePlace">
      <el-input v-model="dataForm.nativePlace" placeholder="学生籍贯"></el-input>
    </el-form-item>
    <el-form-item label="健康状况" prop="health">
      <el-select v-model="dataForm.health" placeholder="请选择">
        <el-option
          v-for="item in healthOptions"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="政治面貌" prop="polity">
      <el-select v-model="dataForm.polity" placeholder="请选择">
        <el-option
          v-for="item in polityOptions"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="班级" prop="classInfoId">
      <el-select v-model="dataForm.classInfoId" placeholder="请选择">
        <el-option
          v-for="item in classInfoOptions"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="指导老师" prop="teaId">
      <el-select v-model="dataForm.teaId" placeholder="请选择">
        <el-option
          v-for="item in teaInfoOptions"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="简介" prop="resume">
      <el-input v-model="dataForm.resume" placeholder="简介"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        classInfoOptions: [],
        teaInfoOptions: [],
        visible: false,
        genderOptions: [
          {
            id: 0,
            name: '男'
          },
          {
            id: 1,
            name: '女'
          }
        ],
        healthOptions: [
          {
            id: 0,
            name: '健康'
          },
          {
            id: 1,
            name: '不健康'
          }
        ],
        polityOptions: [
          {
            id: 0,
            name: '群众'
          },
          {
            id: 1,
            name: '共青团员'
          },
          {
            id: 2,
            name: '中共预备党员'
          },
          {
            id: 3,
            name: '中共预员'
          }
        ],
        dataForm: {
          id: 0,
          no: '',
          name: '',
          sex: '',
          birthday: '',
          nation: '',
          nativePlace: '',
          health: '',
          polity: '',
          classInfoId: '',
          className: '',
          resume: '',
          userId: '',
          teaId: ''
        },
        dataRule: {
          no: [
            { required: true, message: '学号不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '学生姓名不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '学生性别不能为空', trigger: 'blur' }
          ],
          birthday: [
            { required: true, message: '学生出生日期不能为空', trigger: 'blur' }
          ],
          nation: [
            { required: true, message: '民族不能为空', trigger: 'blur' }
          ],
          nativePlace: [
            { required: true, message: '学生籍贯不能为空', trigger: 'blur' }
          ],
          health: [
            { required: true, message: '健康状况不能为空', trigger: 'blur' }
          ],
          polity: [
            { required: true, message: '政治面貌 不能为空', trigger: 'blur' }
          ],
          classInfoId: [
            { required: true, message: '班级信息ID不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created () {
      this.getClassInfoList()
      this.getTeaInfoList()
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/manage/studentinfo/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.no = data.studentInfo.no
                this.dataForm.name = data.studentInfo.name
                this.dataForm.sex = data.studentInfo.sex
                this.dataForm.birthday = data.studentInfo.birthday
                this.dataForm.nation = data.studentInfo.nation
                this.dataForm.nativePlace = data.studentInfo.nativePlace
                this.dataForm.health = data.studentInfo.health
                this.dataForm.polity = data.studentInfo.polity
                this.dataForm.classInfoId = data.studentInfo.classInfoId
                this.dataForm.className = data.studentInfo.className
                this.dataForm.resume = data.studentInfo.resume
                this.dataForm.userId = data.studentInfo.userId
                this.dataForm.teaId = data.studentInfo.teaId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/manage/studentinfo/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'no': this.dataForm.no,
                'name': this.dataForm.name,
                'sex': this.dataForm.sex,
                'birthday': this.dataForm.birthday,
                'nation': this.dataForm.nation,
                'nativePlace': this.dataForm.nativePlace,
                'health': this.dataForm.health,
                'polity': this.dataForm.polity,
                'classInfoId': this.dataForm.classInfoId,
                'className': this.dataForm.className,
                'resume': this.dataForm.resume,
                'userId': this.dataForm.userId,
                'teaId': this.dataForm.teaId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 获取系别列表
      getClassInfoList () {
        this.$http({
          url: this.$http.adornUrl('/manage/classinfo/dropList'),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.classInfoOptions = data.data
            console.log(this.deptIdOptions)
          } else {
            this.classInfoOptions = []
          }
        })
      },
      // 获取教师列表
      getTeaInfoList () {
        this.$http({
          url: this.$http.adornUrl('/manage/teacherinfo/dropList'),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.teaInfoOptions = data.data
            console.log(this.teaInfoOptions)
          } else {
            this.teaInfoOptions = []
          }
        })
      }
    }
  }
</script>
